package com.travel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.travel.pojo.Order;
import com.travel.service.impl.OrderServiceImpl;
import com.travel.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
public class OrderController {
    @Autowired
    private OrderServiceImpl orderService;
    @PostMapping("/order/insert")
    public Result insertOrder(@RequestBody Order order){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        order.setTime(simpleDateFormat.format(new Date()));
        boolean save = orderService.save(order);
        Result<Order> result = new Result<>();
        if (save){
            int i = (int) Math.floor(0 + Math.random() * 9999);
            order.setId(i);
            result.setCode(200);
            result.setMessage("添加订单成功");
        }else {
            result.setCode(500);
            result.setMessage("添加订单失败");
        }
        return result;
    }
    @GetMapping("/order/selectByUserId")
    public Result ordersByUserId(int userId,int status){
        List<Map<String, Object>> maps = orderService.ordersByUserId(userId,status);
        return new Result<>(maps,"查询成功",200);
    }
    @PutMapping("/order/updateStatus")
    public Result updateStatus(int orderId){
        QueryWrapper<Order> updateWrapper = new QueryWrapper<Order>().eq("id", orderId);
        Order order = orderService.getOne(updateWrapper);
        order.setStatus("1");
        boolean update = orderService.update(order, updateWrapper);
        if (update){
            return new Result<>(update,"修改成功",200);
        }else {
            return new Result<>(update,"修改失败",500);
        }
    }
}
